package com.bocaizp.operation.service.impl;

import com.bocaizp.operation.mapper.DataAnalysisMapper;
import com.bocaizp.operation.service.DataAnalysisService;
import com.bocaizp.pojo.DTO.AnalysisHotDTO;
import com.bocaizp.pojo.DTO.AnalysisJobNumberDTO;
import com.bocaizp.pojo.DTO.UserStepDTO;
import com.bocaizp.pojo.VO.AdminDataHierarchyVO;
import com.bocaizp.pojo.VO.AnalysisNameValueVO;
import com.bocaizp.pojo.VO.AnalysisStringVO;
import com.bocaizp.pojo.VO.DataForecastVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Author : huang jie
 * Date : 2024/5/5 22:49
 **/
@Service
@Slf4j
public class DataAnalysisServiceImpl implements DataAnalysisService {

	@Autowired
	private DataAnalysisMapper dataAnalysisMapper;



	/**
	 * 管理端行业预测数据
	 * @return
	 */
	public DataForecastVO getForecastData(String industry) {
		LinkedList<String> dateTime = setDate(11,1);
		int hot = 0, salary=1;
		AnalysisHotDTO data_hotList = dataAnalysisMapper.getForecast(industry, hot);
		AnalysisHotDTO data_salaryList = dataAnalysisMapper.getForecast(industry, salary);
		List<Integer> hotList = backList(data_hotList);
		List<Integer> salaryList = backList(data_salaryList);
		return DataForecastVO.builder()
				.dateTime(StringUtils.join(dateTime,","))
				.hotList(StringUtils.join(hotList,","))
				.salaryList(StringUtils.join(salaryList,","))
				.build();
	}

	private List<Integer> backList(AnalysisHotDTO analysisHotDTO){
		List<Integer> list = new ArrayList<>();
		list.add(analysisHotDTO.getDateA());
		list.add(analysisHotDTO.getDateB());
		list.add(analysisHotDTO.getDateC());
		list.add(analysisHotDTO.getDateD());
		list.add(analysisHotDTO.getDateE());
		list.add(analysisHotDTO.getDateF());
		list.add(analysisHotDTO.getDateG());
		list.add(analysisHotDTO.getFutA());
		list.add(analysisHotDTO.getFutB());
		list.add(analysisHotDTO.getFutC());
		list.add(analysisHotDTO.getFutD());
		return list;
	}


	/**
	 * 获取行业平均薪资
	 * @return
	 */
	public AnalysisStringVO getAvgSalary() {
		int status = 1;
		List<AnalysisNameValueVO> analysisNameValueVOS = dataAnalysisMapper.getAvgSalary(status);
		List<String> name = new ArrayList<>();
		List<Integer> value = new ArrayList<>();
		for(AnalysisNameValueVO an : analysisNameValueVOS){
			name.add(an.getName());
			value.add(an.getValue());
		}
		return AnalysisStringVO.builder()
				.name(StringUtils.join(name,","))
				.value(StringUtils.join(value,","))
				.build();
	}

	/**
	 * 岗位总数变化
	 * @return
	 */
	public AnalysisStringVO getJobNumber() {
		LinkedList<String> date = setDate(8, 1);
		List<AnalysisJobNumberDTO> analysisJobNumberDTO = dataAnalysisMapper.getJobNumber();
		LinkedList<Integer> data = backList(analysisJobNumberDTO.get(0));
		LinkedList<String> up = new LinkedList<>();
		LinkedList<String> down = new LinkedList<>();
		LinkedList<Integer> dataList = new LinkedList<>();
		int cnt = analysisJobNumberDTO.get(0).getDataA();
		for(int s : data){
			if(cnt <= s){
				//升
				dataList.addLast(cnt);
				up.addLast(String.valueOf(s - cnt));
				down.addLast("-");
			}else{
				//降
				dataList.addLast(s);
				up.addLast("-");
				down.addLast(String.valueOf(cnt - s));
			}
			cnt = s;
		}
		return AnalysisStringVO.builder()
				.date(StringUtils.join(date,","))
				.name(StringUtils.join(dataList, ","))
				.value(StringUtils.join(up, ","))
				.pValue(StringUtils.join(down, ","))
				.build();
	}

	/**
	 * 获取行业热度
	 * @return
	 */
	public AnalysisStringVO getIndustryHot() {
		int status = 0;
		List<AnalysisNameValueVO> data = dataAnalysisMapper.getDataHot(status);
		List<String> name = new ArrayList<>();
		List<Integer> value = new ArrayList<>();
		for (AnalysisNameValueVO an: data){
			name.add(an.getName());
			value.add(an.getValue());
		}
		return AnalysisStringVO.builder()
				.name(StringUtils.join(name,","))
				.value(StringUtils.join(value,","))
				.build();
	}

	/**
	 * 获取用户分层数据
	 * @return
	 */
	public AdminDataHierarchyVO getStepData() {
		LinkedList<String> date = setDate(6, 2);
		LinkedList<String> dateTime = new LinkedList<>();
		dateTime.add("product");
		dateTime.addAll(date);
		List<UserStepDTO> dataList = dataAnalysisMapper.getUserData();
		List<String> data1 = new ArrayList<>();
		List<String> data2 = new ArrayList<>();
		List<String> data3 = new ArrayList<>();
		List<String> data4 = new ArrayList<>();
		List<String> data5 = new ArrayList<>();
		List<String> data6 = new ArrayList<>();
		List<String> data7 = new ArrayList<>();
		List<String> data8 = new ArrayList<>();
		data1.add("重要价值用户");
		data2.add("一般价值用户");
		data3.add("重要发展用户");
		data4.add("一般发展用户");
		data5.add("重要保持用户");
		data6.add("一般保持用户");
		data7.add("重要挽留用户");
		data8.add("一般挽留用户");
		for (UserStepDTO u : dataList){
			data1.add(String.valueOf(u.getValueA()));
			data2.add(String.valueOf(u.getValueB()));
			data3.add(String.valueOf(u.getValueC()));
			data4.add(String.valueOf(u.getValueD()));
			data5.add(String.valueOf(u.getValueE()));
			data6.add(String.valueOf(u.getValueF()));
			data7.add(String.valueOf(u.getValueG()));
			data8.add(String.valueOf(u.getValueH()));
		}
		return AdminDataHierarchyVO.builder()
				.dateTime(StringUtils.join(dateTime,","))
				.dataOne(StringUtils.join(data1,","))
				.dataTwo(StringUtils.join(data2,","))
				.dataThree(StringUtils.join(data3,","))
				.dataFour(StringUtils.join(data4,","))
				.dataFive(StringUtils.join(data5,","))
				.dataSix(StringUtils.join(data6,","))
				.dataSeven(StringUtils.join(data7,","))
				.dataEight(StringUtils.join(data8,","))
				.build();
	}

	/**
	 * 更新数据时，获取旧数据
	 * @param a
	 * @return
	 */
	public LinkedList<Integer> backList(AnalysisJobNumberDTO a){
		LinkedList<Integer> data = new LinkedList<>();
		data.addLast(a.getDataB());
		data.addLast(a.getDataC());
		data.addLast(a.getDataD());
		data.addLast(a.getDataE());
		data.addLast(a.getDataF());
		data.addLast(a.getDataG());
		data.addLast(a.getDataH());
		data.addLast(a.getDataI());
		return data;
	}

	/**
	 * 计算时间日期 返回(yyyy-MM)型日期
	 * @param num
	 * @return
	 */
	public LinkedList<String> setDate(int num, int res){
		num *= res;
		LinkedList<String> dates = new LinkedList<>();
		LocalDate localDate = LocalDate.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

		for ( int i = num; i > 0; i -= res ){
			LocalDate localDate1 = localDate.minusMonths(i);
			String emp = localDate1.format(formatter);
			dates.addLast(emp);
		}
		return dates;
	}


}
